package jsqlite;

import com.github.mikephil.charting.utils.Utils;
import java.io.PrintStream;

/* loaded from: classes2.dex */
public class Database {
    protected long handle = 0;
    protected int error_code = 0;

    private static native void _backup(Backup backup, Database database, String str, Database database2, String str2) throws Exception;

    private native void _busy_handler(BusyHandler busyHandler);

    private native void _busy_timeout(int i);

    private native long _changes();

    private native void _close() throws Exception;

    private static native boolean _complete(String str);

    private native void _create_aggregate(String str, int i, Function function);

    private native void _create_function(String str, int i, Function function);

    private native int _db_status(int i, int[] iArr, boolean z);

    /* JADX INFO: Access modifiers changed from: protected */
    public static native boolean _enable_shared_cache(boolean z);

    private native String _errmsg();

    private native void _exec(String str, Callback callback) throws Exception;

    private native void _exec(String str, Callback callback, String[] strArr) throws Exception;

    private native void _finalize();

    private native void _function_type(String str, int i);

    private native void _interrupt();

    private native void _key(byte[] bArr);

    private native long _last_insert_rowid();

    private native void _open(String str, int i) throws Exception;

    private native void _open4(String str, int i, String str2, boolean z) throws Exception;

    private native void _open_aux_file(String str) throws Exception;

    private native void _open_blob(String str, String str2, String str3, long j, boolean z, Blob blob) throws Exception;

    private native void _profile(Profile profile);

    private native void _progress_handler(int i, ProgressHandler progressHandler);

    private native void _rekey(byte[] bArr);

    private native void _set_authorizer(Authorizer authorizer);

    private native void _set_encoding(String str) throws Exception;

    private static native int _status(int i, int[] iArr, boolean z);

    private native void _trace(Trace trace);

    public static native String error_string(int i);

    private static native void internal_init();

    public static long long_from_julian(double d) {
        return (long) ((d - 2440587.5d) * 8.64E7d);
    }

    private native void stmt_prepare(String str, Stmt stmt) throws Exception;

    public static native String version();

    private native void vm_compile(String str, Vm vm) throws Exception;

    private native void vm_compile_args(String str, Vm vm, String[] strArr) throws Exception;

    public native String dbversion();

    public native boolean is3();

    public native void spatialite_create();

    public void open(String str, int i) throws Exception {
        if ((i & 128) != 0) {
            i = 6;
        } else if ((i & 256) != 0) {
            i = 1;
        }
        synchronized (this) {
            try {
                try {
                    _open4(str, i, null, false);
                } catch (OutOfMemoryError e) {
                    throw e;
                } catch (Throwable unused) {
                    _open(str, i);
                }
            } catch (Exception e2) {
                throw e2;
            }
        }
    }

    public void open(String str, int i, String str2) throws Exception {
        if ((i & 128) != 0) {
            i = 6;
        } else if ((i & 256) != 0) {
            i = 1;
        }
        synchronized (this) {
            try {
                _open4(str, i, str2, false);
            } catch (OutOfMemoryError e) {
                throw e;
            } catch (Exception e2) {
                throw e2;
            } catch (Throwable unused) {
                _open(str, i);
            }
        }
    }

    public void open(String str, int i, String str2, boolean z) throws Exception {
        if ((i & 128) != 0) {
            i = 6;
        } else if ((i & 256) != 0) {
            i = 1;
        }
        synchronized (this) {
            try {
                try {
                    _open4(str, i, str2, z);
                } catch (OutOfMemoryError e) {
                    throw e;
                } catch (Throwable unused) {
                    _open(str, i);
                }
            } catch (Exception e2) {
                throw e2;
            }
        }
    }

    public void open_aux_file(String str) throws Exception {
        synchronized (this) {
            _open_aux_file(str);
        }
    }

    protected void finalize() {
        synchronized (this) {
            _finalize();
        }
    }

    public void close() throws Exception {
        synchronized (this) {
            _close();
        }
    }

    public void exec(String str, Callback callback) throws Exception {
        synchronized (this) {
            _exec(str, callback);
        }
    }

    public void exec(String str, Callback callback, String[] strArr) throws Exception {
        synchronized (this) {
            _exec(str, callback, strArr);
        }
    }

    public long last_insert_rowid() {
        long _last_insert_rowid;
        synchronized (this) {
            _last_insert_rowid = _last_insert_rowid();
        }
        return _last_insert_rowid;
    }

    public void interrupt() {
        synchronized (this) {
            _interrupt();
        }
    }

    public long changes() {
        long _changes;
        synchronized (this) {
            _changes = _changes();
        }
        return _changes;
    }

    public void busy_handler(BusyHandler busyHandler) {
        synchronized (this) {
            _busy_handler(busyHandler);
        }
    }

    public void busy_timeout(int i) {
        synchronized (this) {
            _busy_timeout(i);
        }
    }

    public TableResult get_table(String str, int i) throws Exception {
        TableResult tableResult = new TableResult(i);
        if (!is3()) {
            try {
                exec(str, tableResult);
            } catch (Exception e) {
                if (i <= 0 || !tableResult.atmaxrows) {
                    throw e;
                }
            }
        } else {
            synchronized (this) {
                Vm compile = compile(str);
                set_last_error(compile.error_code);
                if (tableResult.maxrows > 0) {
                    while (tableResult.nrows < tableResult.maxrows && compile.step(tableResult)) {
                        set_last_error(compile.error_code);
                    }
                } else {
                    while (compile.step(tableResult)) {
                        set_last_error(compile.error_code);
                    }
                }
                compile.finalize();
            }
        }
        return tableResult;
    }

    public TableResult get_table(String str) throws Exception {
        return get_table(str, 0);
    }

    public TableResult get_table(String str, int i, String[] strArr) throws Exception {
        TableResult tableResult = new TableResult(i);
        if (!is3()) {
            try {
                exec(str, tableResult, strArr);
            } catch (Exception e) {
                if (i <= 0 || !tableResult.atmaxrows) {
                    throw e;
                }
            }
        } else {
            synchronized (this) {
                Vm compile = compile(str, strArr);
                set_last_error(compile.error_code);
                if (tableResult.maxrows > 0) {
                    while (tableResult.nrows < tableResult.maxrows && compile.step(tableResult)) {
                        set_last_error(compile.error_code);
                    }
                } else {
                    while (compile.step(tableResult)) {
                        set_last_error(compile.error_code);
                    }
                }
                compile.finalize();
            }
        }
        return tableResult;
    }

    public TableResult get_table(String str, String[] strArr) throws Exception {
        return get_table(str, 0, strArr);
    }

    public void get_table(String str, String[] strArr, TableResult tableResult) throws Exception {
        tableResult.clear();
        if (!is3()) {
            try {
                exec(str, tableResult, strArr);
                return;
            } catch (Exception e) {
                if (tableResult.maxrows <= 0 || !tableResult.atmaxrows) {
                    throw e;
                }
                return;
            }
        }
        synchronized (this) {
            Vm compile = compile(str, strArr);
            if (tableResult.maxrows > 0) {
                while (tableResult.nrows < tableResult.maxrows && compile.step(tableResult)) {
                    set_last_error(compile.error_code);
                }
            } else {
                while (compile.step(tableResult)) {
                    set_last_error(compile.error_code);
                }
            }
            compile.finalize();
        }
    }

    public static synchronized boolean complete(String str) {
        boolean _complete;
        synchronized (Database.class) {
            _complete = _complete(str);
        }
        return _complete;
    }

    public void create_function(String str, int i, Function function) {
        synchronized (this) {
            _create_function(str, i, function);
        }
    }

    public void create_aggregate(String str, int i, Function function) {
        synchronized (this) {
            _create_aggregate(str, i, function);
        }
    }

    public void function_type(String str, int i) {
        synchronized (this) {
            _function_type(str, i);
        }
    }

    public int last_error() {
        return this.error_code;
    }

    protected void set_last_error(int i) {
        this.error_code = i;
    }

    public String error_message() {
        String _errmsg;
        synchronized (this) {
            _errmsg = _errmsg();
        }
        return _errmsg;
    }

    public void set_encoding(String str) throws Exception {
        synchronized (this) {
            _set_encoding(str);
        }
    }

    public void set_authorizer(Authorizer authorizer) {
        synchronized (this) {
            _set_authorizer(authorizer);
        }
    }

    public void trace(Trace trace) {
        synchronized (this) {
            _trace(trace);
        }
    }

    public Backup backup(Database database, String str, String str2) throws Exception {
        Backup backup;
        synchronized (this) {
            backup = new Backup();
            _backup(backup, database, str, this, str2);
        }
        return backup;
    }

    public void profile(Profile profile) {
        synchronized (this) {
            _profile(profile);
        }
    }

    public static synchronized int status(int i, int[] iArr, boolean z) {
        int _status;
        synchronized (Database.class) {
            _status = _status(i, iArr, z);
        }
        return _status;
    }

    public int db_status(int i, int[] iArr, boolean z) {
        int _db_status;
        synchronized (this) {
            _db_status = _db_status(i, iArr, z);
        }
        return _db_status;
    }

    public Vm compile(String str) throws Exception {
        Vm vm;
        synchronized (this) {
            vm = new Vm();
            vm_compile(str, vm);
        }
        return vm;
    }

    public Vm compile(String str, String[] strArr) throws Exception {
        Vm vm;
        synchronized (this) {
            vm = new Vm();
            vm_compile_args(str, vm, strArr);
        }
        return vm;
    }

    public Stmt prepare(String str) throws Exception {
        Stmt stmt;
        synchronized (this) {
            stmt = new Stmt();
            stmt_prepare(str, stmt);
        }
        return stmt;
    }

    public Blob open_blob(String str, String str2, String str3, long j, boolean z) throws Exception {
        Blob blob;
        synchronized (this) {
            blob = new Blob();
            _open_blob(str, str2, str3, j, z, blob);
        }
        return blob;
    }

    public void progress_handler(int i, ProgressHandler progressHandler) {
        synchronized (this) {
            _progress_handler(i, progressHandler);
        }
    }

    public void key(byte[] bArr) throws Exception {
        synchronized (this) {
            _key(bArr);
        }
    }

    public void key(String str) throws Exception {
        synchronized (this) {
            byte[] bArr = null;
            if (str != null && str.length() > 0) {
                bArr = new byte[str.length()];
                for (int i = 0; i < str.length(); i++) {
                    char charAt = str.charAt(i);
                    bArr[i] = (byte) ((charAt >> '\b') ^ (charAt & 255));
                }
            }
            _key(bArr);
        }
    }

    public void rekey(byte[] bArr) throws Exception {
        synchronized (this) {
            _rekey(bArr);
        }
    }

    public void rekey(String str) throws Exception {
        synchronized (this) {
            byte[] bArr = null;
            if (str != null && str.length() > 0) {
                bArr = new byte[str.length()];
                for (int i = 0; i < str.length(); i++) {
                    char charAt = str.charAt(i);
                    bArr[i] = (byte) ((charAt >> '\b') ^ (charAt & 255));
                }
            }
            _rekey(bArr);
        }
    }

    public static long long_from_julian(String str) throws Exception {
        try {
            return long_from_julian(Double.valueOf(str).doubleValue());
        } catch (java.lang.Exception e) {
            throw new Exception("not a julian date: " + str + ": " + e);
        }
    }

    public static double julian_from_long(long j) {
        double d = j < 0 ? Utils.DOUBLE_EPSILON : 0.5d;
        double d2 = j;
        Double.isNaN(d2);
        return ((d2 + d) / 8.64E7d) + 2440587.5d;
    }

    static {
        try {
            System.loadLibrary("jsqlite");
        } catch (Throwable th) {
            PrintStream printStream = System.err;
            printStream.println("Unable to load sqlite_jni: " + th);
        }
        try {
            internal_init();
            new FunctionContext();
        } catch (java.lang.Exception unused) {
        }
    }
}
